package ru.gosoplata.model 
{
	import ru.gosoplata.model.vo.Commission;
	/**
	 * ...
	 * @author 
	 */
	public class Payment 
	{		
		private var _operation:String;
		private var _amount:uint=0;// внесенная сумма
		private var _provider:Provider = new Provider();// PrvId PrvName
		private var _accountId:String="";//AccNum - телефон
		private var _commission:Commission = new Commission();
		private var _forEnrollment:Number;
		private var _payBack:int;
		private var _pages:Array=[];
		private var _actions:Array=[];
		private var _fixPrice:uint=0;
		
		public function Payment() 
		{
		}
		
		public function get accauntnumber():String //TODO: дублирование. это текст для отображения. сделать по человечески.
		{
			return _accauntnumber==""?_accountId+"":_accauntnumber;
		}
		public function set accauntnumber(value:String):void 
		{
			_accauntnumber = value;
		}
		
		public function get saveData():Object 
		{
			var o:Object = { };
			o.operation = operation;
			//o.payBack = payBack;
			//o.actions = _actions;
			o.pages = _pages;
			return o;
		}
		public function set saveData(o:Object):void 
		{
			TmpModel.debugOutput.add("Payment saveData: " + JSON.stringify(o) );
			
			operation = o.operation;
			_actions=o.actions;
			_pages = o.pages;
			_payBack = o.payBack;
			
			if (!_actions)_actions = [];
			if (!_pages)_pages = [];
			
			if (!operation || operation.length < 5) 
			{
				_actions = [];//TODO: не уверен что это нужно
				_pages = [];
			}
			
			TmpModel.debugOutput.add("Payment saveData: operation=" + operation + ' ,_actions=' + _actions + ' ,_pages=' + _pages );
		}
		
		public function get provider():Provider 
		{
			return _provider;
		}
		
		public function get accountId():String 
		{
			return _accountId;
		}
		
		public function get fixPrice():uint 
		{
			return Math.max(_fixPrice,_provider.fixPrice);
		}
		
		public function get payBack():uint 
		{
			return _payBack;
		}
		
		public function set payBack(val:uint):void 
		{
			// TODO: нужно ли? сохранить текущий платеж
			_payBack = val;
			if (_payBack < 0)
			{
				_payBack = 0;
				//TODO: записать ошибку!!!!
			}
			if (_payBack == 0) operation = '';
		}
		
		public function get amount():uint 
		{
			return _amount;
		}
		
		public function set amount(value:uint):void 
		{
			// TODO: нужно ли? сохранить текущий платеж
			_amount = value;
			_commission = provider.getCommission(value);
			_forEnrollment = _amount - _commission.val;
			_payBack = 0;
			if (fixPrice > 0) 
			{
				if (_forEnrollment < fixPrice) 
				{
					_forEnrollment = 0;
					_payBack = _amount;
				}else if (_forEnrollment > fixPrice) 
				{
					//TODO: есть еще параметр use_change у провайдера
					_payBack = _forEnrollment - fixPrice;
					_forEnrollment = fixPrice;
				}
			}
			if (_forEnrollment <= 0) {
				_forEnrollment = 0;
				_payBack = _amount;
			}
			TmpModel.tmpGlobalController.mediatorCashSumm();//TODO: вещаем событие)
		}
		
		public function get commission():Commission 
		{
			return _commission;
		}
		
		public function get forEnrollment():Number 
		{
			return _forEnrollment;
		}
		
		public function get minPay():Number
		{
			return Math.max(0,fixPrice);
		}
		
		public function get operation():String 
		{
			return _operation;
		}
		
		public function set operation(value:String):void 
		{
			TmpModel.debugOutput.add("operation = "+value);
			_operation = value;
		}
		
		public function toProvider(id:String):void 
		{
			_accountId=_accauntnumber="";
			
			_amount = 0;
			_commission = new Commission();
			_forEnrollment = 0;
			
			_provider.id = id;
		}
		
		public function validateAccount(value:String):void 
		{
			_accountId = "";
			if (_provider.validateAccount(value))
			{
				_accountId = value;
			}
		}
		
		public function serchProvider(str:String):void 
		{
			_accountId=_accauntnumber="";
			
			_amount = 0;
			_commission = new Commission();
			_forEnrollment = 0;
			
			provider.serch(str);
		}
		
		public function addPageToShow(page:String):void 
		{
			TmpModel.debugOutput.add("addPageToShow "+page+" "+_pages.join(","))
			_pages.push(page);
		}
		
		public function addAction(action:String):void 
		{
			_actions.push(action);
		}
		
		public function getAction():String 
		{
			return _actions.length?_actions.pop():"";
		}
		
		public function getPage():String 
		{
			return _pages.length?_pages.pop():"";
		}
		
		public function getCreatPayString():String 
		{
			var str:String = "AccNum=" + accountId + "&PrvId=" + provider.providerID;
			if (fixPrice > 0) str += "&_extra_fixed_int_summ=" + fixPrice;
			
			return str
		}
		
		public function timeoutPay():void 
		{
			_operation = "";
			_forEnrollment = 0;
			if (fixPrice > 0 && amount>0) 
			{
				_fixPrice = 100000;
				_payBack = 0;
			}
			
			_pages = ["index"];
		}
		
		
		
		
		
		//TODO: tmp _extraData - удалить потом
		private var _accauntnumber:String = "";
		private var _extraData:Array = new Array();
		
		public function clearExtraData():void {
			_extraData = new Array();
		}
		public function addExtraDataField(ed_name:String, ed_data:String):void { 
			_extraData.push(new Array(ed_name, ed_data));
		}
		public function getExtraDataLengthByID(id:Number):Number {
			return String(_extraData[id][1]).length;
		}
		public function getExtraDataNameByID(id:Number):String {
			return _extraData[id][0];
		}
		public function getExtraDataIDByName(ed_name:String):Number {
			for (var ii:Number = 0; ii < _extraData.length; ii++ ) {
				if (_extraData[ii][0] == ed_name) return ii;
			}
			return -1;
		}
		public function writeExtrDataByID(id:Number,data:String):void {
			_extraData[id][1] = data;
		}
		public function getExtraDataByID(id:Number):String { 
			return _extraData[id][1];
		}
		public function getExtraDataLengthByName(ed_name:String):Number {
			for (var ii:Number = 0; ii < _extraData.length; ii++ ) {
				if (_extraData[ii][0] == ed_name) return String(_extraData[ii][1]).length;
			}
			return -1;
		}
		
		public function getExtraFieldsString():String {
			var str:String = "";
			for (var ii:Number = 0; ii < _extraData.length; ii++ ) {
				if (String(_extraData[ii][1]).length > 0 && String(_extraData[ii][0]).indexOf("extra") > 0 ) {
					str += "&" + _extraData[ii][0] + "=" + _extraData[ii][1];
				}
			}
			return str;
		}
	}
}